# -*- coding: utf-8 -*-

from django.shortcuts import render_to_response
from django.forms import ModelForm
from django.http import HttpResponse, HttpResponseRedirect
from apps.prewarning.models import *

def modelset(request):
    id = request.GET.get('id')
    modelset_list = ModelSet.objects.all()
    return render_to_response('tab_template/alertsystem/alert/model_line.html',
    {'modelset_list': modelset_list, 'id': id})

class ModelSetForm(ModelForm):
    class Meta:
        model = ModelSet
        exclude = ('model', )

def setmodelset(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        value = request.POST.get('value')
        modelset = ModelSet.objects.get(pk=int(id))
        form = ModelSetForm(request.POST, instance=modelset)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.value = value
            instance.save()
        return HttpResponseRedirect('/prewarning/?id=%d' % int(id))
    